using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace FiltronASP.Helpers.StringExtensions
{
    public static class StringExtension
    {
        /// <summary>
        /// Metoda usuwajaca polskie znaki z zadanego tekstu.
        /// </summary>
        /// <param name="text">tekst, z ktorego usunac polskie znaki</param>
        /// <returns>tekst bez polskich znakow</returns>
        public static string RemovePolishChars(this string text)
        {
            if (String.IsNullOrEmpty(text))
            {
                return text;
            }   //if
            char[] chars = text.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
	            switch (chars[i])
	            {
		            case 'ą': chars[i] = 'a'; break;
		            case 'ć': chars[i] = 'c'; break;
		            case 'ę': chars[i] = 'e'; break;
		            case 'ł': chars[i] = 'l'; break;
		            case 'ń': chars[i] = 'n'; break;
		            case 'ó': chars[i] = 'o'; break;
		            case 'ś': chars[i] = 's'; break;
		            case 'ż': chars[i] = 'z'; break;
		            case 'ź': chars[i] = 'z'; break;
		            case 'Ą': chars[i] = 'A'; break;
                    case 'Ć': chars[i] = 'C'; break;
		            case 'Ę': chars[i] = 'E'; break;
		            case 'Ł': chars[i] = 'L'; break;
		            case 'Ń': chars[i] = 'N'; break;
		            case 'Ó': chars[i] = 'O'; break;
		            case 'Ś': chars[i] = 'S'; break;
		            case 'Ż': chars[i] = 'Z'; break;
		            case 'Ź': chars[i] = 'Z'; break;
                    case 'á': chars[i] = 'a'; break; 
                    case 'é': chars[i] = 'e'; break;
                    case 'í': chars[i] = 'i'; break;
                    case 'ö': chars[i] = 'o'; break;
                    case 'ő': chars[i] = 'o'; break;
                    case 'ü': chars[i] = 'u'; break;
                    case 'ú': chars[i] = 'u'; break;
                    case 'ű': chars[i] = 'u'; break;
                    case 'Á': chars[i] = 'A'; break;
                    case 'É': chars[i] = 'E'; break;
                    case 'Í': chars[i] = 'I'; break;
                    case 'Ö': chars[i] = 'O'; break;
                    case 'Ő': chars[i] = 'O'; break;
                    case 'Ü': chars[i] = 'U'; break;
                    case 'Ú': chars[i] = 'U'; break;
                    case 'Ű': chars[i] = 'U'; break;
                    case 'б': chars[i] = 'b'; break;
                    case 'в': chars[i] = 'v'; break;
                    case 'г': chars[i] = 'g'; break;
                    case 'д': chars[i] = 'd'; break;
                    case 'ж': chars[i] = 'X'; break;
                    case 'з': chars[i] = 'z'; break;
                    case 'и': chars[i] = 'i'; break;
                    case 'й': chars[i] = 'y'; break;
                    case 'к': chars[i] = 'k'; break;
                    case 'л': chars[i] = 'l'; break;
                    case 'м': chars[i] = 'm'; break;
                    case 'н': chars[i] = 'n'; break;
                    case 'п': chars[i] = 'r'; break;
                    case 'т': chars[i] = 't'; break;
                    case 'у': chars[i] = 'u'; break;
                    case 'ф': chars[i] = 'f'; break;
                    case 'ц': chars[i] = 'u'; break;
                    case 'ч': chars[i] = 'h'; break;
                    case 'ш': chars[i] = 'm'; break;
                    case 'щ': chars[i] = 'm'; break;
                    case 'ъ': chars[i] = 'a'; break;
                    case 'ь': chars[i] = 'y'; break;
                    case 'ю': chars[i] = 'o'; break;
                    case 'я': chars[i] = 'R'; break;
                    case 'Б': chars[i] = 'B'; break;
                    case 'Г': chars[i] = 'G'; break;
                    case 'Д': chars[i] = 'D'; break;
                    case 'Ж': chars[i] = 'X'; break;
                    case 'З': chars[i] = 'Z'; break;
                    case 'И': chars[i] = 'I'; break;
                    case 'Й': chars[i] = 'Y'; break;
                    case 'К': chars[i] = 'K'; break;
                    case 'Л': chars[i] = 'L'; break;
                    case 'П': chars[i] = 'R'; break;
                    case 'У': chars[i] = 'U'; break;
                    case 'Ф': chars[i] = 'F'; break;
                    case 'Ц': chars[i] = 'U'; break;
                    case 'Ч': chars[i] = 'h'; break;
                    case 'Ш': chars[i] = 'M'; break;
                    case 'Щ': chars[i] = 'M'; break;
                    case 'Ъ': chars[i] = 'A'; break;
                    case 'Ь': chars[i] = 'Y'; break;
                    case 'Ю': chars[i] = 'O'; break;
                    case 'Я': chars[i] = 'R'; break;
                    case 'р': chars[i] = 'p'; break;
                    case 'е': chars[i] = 'e'; break;
                    case 'о': chars[i] = 'o'; break;
                    case 'а': chars[i] = 'a'; break;
                    case 'с': chars[i] = 'c'; break;
                    case 'М': chars[i] = 'M'; break;
                    case 'ы': chars[i] = 'b'; break;
                    case 'Т': chars[i] = 'T'; break;
                        

	            }   //switch
            }   //for
            return new string(chars);
        }

        /// <summary>
        /// Metoda konwertujaca zadany tekst do urla (usuwanie znakow specjalnych, url encoding).
        /// </summary>
        /// <param name="text">tekst do konwersji</param>
        /// <returns>string nadajacy sie jako url</returns>
        public static string RewriteToUrl(this string text)
        {
            if (String.IsNullOrEmpty(text))
            {
                throw new Exception("StringExtension exception: RewriteToUrl method, text to convert is blank..."); 
            }   //if
            char[] chars = text.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
                switch (chars[i])
                {
                    case 'ˇ': chars[i] = 'A'; break;
                    case '¦': chars[i] = 'S'; break;
                    case '¬': chars[i] = 'Z'; break;
                    case '¶': chars[i] = 's'; break;
                    case 'Ľ': chars[i] = 'z'; break;
                    case '±': chars[i] = 'a'; break;
                }   //switch
            }   //for
            return HttpUtility.UrlEncode(Regex.Replace(text, @"[\s\p{P}\p{S}-[._]]", "-").ToLower());
        }

        public static bool ifLastDigitIsOdd(string givenString)
        {
            if (givenString == String.Empty)
            {
                return false;
            }
            string givenStringLastChar = givenString.Substring((givenString.Length - 1), 1);
            char givenStringChar = Convert.ToChar(givenStringLastChar);
            if (char.IsDigit(givenStringChar))
            {
                int givenStringInt = Convert.ToInt32(givenStringLastChar);
                if (givenStringInt % 2 == 0)
                {
                    return true;
                }
            }
            else { 
                // jeśli nie jest cyfrą
                string givenStringWithoutLastChar = givenString.Substring(0, (givenString.Length - 1));
                return Helpers.StringExtensions.StringExtension.ifLastDigitIsOdd(givenStringWithoutLastChar);
            }            
            return false;
        }

    }
}